package ch.zhaw.skuh.turing.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Panel;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JTextField;

import ch.zhaw.skuh.turing.controller.Controller;
import ch.zhaw.skuh.turing.model.turingmaschine.TuringChar;

public class ControlPanel extends Panel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3540411182483585449L;
	private Panel panelOperation;
	private JTextField field;
	private Controller controller;
	private List<StepButton> stepButton;
	private StepButton runButton;
	private List<OperationButton> listButtons;
	private OperationButton currentOperation;

	public ControlPanel(Controller ctrl) {
		this.controller = ctrl;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		field = new JTextField("1 1");
		Dimension dim = new Dimension();
		dim.setSize(field.getMaximumSize().getWidth(), field.getPreferredSize()
				.getHeight());
		field.setMaximumSize(dim);
		add(field);

		panelOperation = new Panel();
		loadOperations();
		add(panelOperation);

		Panel panelRun = new Panel();
		panelRun.setLayout(new BoxLayout(panelRun, BoxLayout.X_AXIS));
		
		// step buttons
		this.stepButton = new ArrayList<StepButton>();
		stepButton.add(new StepButton(ctrl, 1, "Step"));
		stepButton.add(new StepButton(ctrl, 10, "Step 10"));
		stepButton.add(new StepButton(ctrl, 100, "Step 100"));
		// add StepButtons
		for(StepButton sb : stepButton){
			panelRun.add(sb);
		}
		
		// Run button
		runButton = new StepButton(ctrl, 0, "Run");
		panelRun.add(runButton);
		
		enableRun(false);

		add(panelRun);
	}
	
	protected void setOperationButton(OperationButton newOB){
		if(currentOperation != null){
			currentOperation.setBackground(Color.LIGHT_GRAY);
		}
		currentOperation = newOB;
		currentOperation.setBackground(Color.CYAN);
	}

	public void enableRun(boolean set) {
		for(StepButton sb : stepButton){
			sb.setEnabled(set);
		}
		runButton.setEnabled(set);
	}

	private void loadOperations() {
		panelOperation
				.setLayout(new BoxLayout(panelOperation, BoxLayout.X_AXIS));
		panelOperation.removeAll();
		
		listButtons = new	ArrayList<OperationButton>(); 
		
		for (TuringChar turingChar : controller.getDisplayTuring()) {
			OperationButton ob = new OperationButton(controller, turingChar, field, this);
			listButtons.add(ob);
			panelOperation.add(ob);
		}
	}

}
